package com.ztgf.file.utils;

import com.ztgf.common.utils.StrUtils;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;

@Slf4j
public class MarkImgUtils {

    /**
     * 添加图片水印
     *
     * @param targetImg 目标图片路径，如：C://myPictrue//1.jpg
     * @param waterImg 水印图片路径，如：C://myPictrue//logo.png
     * @param x 水印图片距离目标图片左侧的偏移量，如果x<0, 则在正中间
     * @param y 水印图片距离目标图片上侧的偏移量，如果y<0, 则在正中间
     * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明)
     * @param degree 水印图片旋转角度
     */
    public static void pressImage(String targetImg, String waterImg,  int x, int y, float alpha, Integer degree,String suffix) {
        pressImage(targetImg,waterImg,null,x,y,alpha,null,suffix);
    }

    /**
     * 添加图片水印
     * @param targetImg 目标图片路径，如：C://myPictrue//1.jpg
     * @param waterImg 水印图片路径，如：C://myPictrue//logo.png
     * @param outImg 图片输出位置，如果为空，则覆盖原文件
     * @param x 水印图片距离目标图片左侧的偏移量，如果x<0, 则在正中间
     * @param y 水印图片距离目标图片上侧的偏移量，如果y<0, 则在正中间
     * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明)
     * @param degree 水印图片旋转角度
     */
    public static void pressImage(String targetImg, String waterImg, String outImg, int x, int y, float alpha, Integer degree,String suffix) {
        try {
            File file = new File(targetImg);
            //如果没有指定文件存放地址，则默认替换掉原图片
            File outFile;
            if(StrUtils.isEmpty(outImg)) {
                outFile = file;
            } else {
                outFile = new File(outImg);
            }

            Image image = null;
            try {
                image = ImageIO.read(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            int width = image.getWidth(null);
            int height = image.getHeight(null);
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image, 0, 0, width, height, null);

            if (null != degree) {
                // 设置水印旋转
                g.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
            }

            Image waterImage = ImageIO.read(new File(waterImg));    // 水印文件
            int width_1 = waterImage.getWidth(null);
            int height_1 = waterImage.getHeight(null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

            int widthDiff = width - width_1;
            int heightDiff = height - height_1;
            if (x < 0) {
                x = widthDiff / 2;
            } else if (x > widthDiff) {
                x = widthDiff;
            }
            if (y < 0) {
                y = heightDiff / 2;
            } else if (y > heightDiff) {
                y = heightDiff;
            }
            g.drawImage(waterImage, x, y, width_1, height_1, null); // 水印文件结束
            g.dispose();
            //注意这里的后缀不能带 .
            ImageIO.write(bufferedImage, suffix.substring(1), outFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 添加文字水印
     *
     * @param targetImg 目标图片路径，如：C://myPictrue//1.jpg
     * @param pressText 水印文字， 如：云账房
     * @param fontName 字体名称，    如：宋体
     * @param fontStyle 字体样式，如：粗体和斜体(Font.BOLD|Font.ITALIC)
     * @param fontSize 字体大小，单位为像素
     * @param color 字体颜色
     * @param x 水印文字距离目标图片左侧的偏移量，如果x<0, 则在正中间
     * @param y 水印文字距离目标图片上侧的偏移量，如果y<0, 则在正中间
     * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明)
     */
    public static void pressText(String targetImg, String pressText, String fontName, int fontStyle, int fontSize, Color color, int x, int y, float alpha,String suffix) {
        pressText(targetImg,pressText,null,fontName,fontStyle,fontSize,color,x,y,alpha,null,suffix);
    }

    /**
     * 添加文字水印
     * @Description 坐标(0,0)在图片的左上角，(负数，负数)在中心位置，(0,xxx)在左边，(x,0)在最上边，其他位置根据图片来确定
     * @param targetImg 目标图片路径，如：C://myPictrue//1.jpg
     * @param pressText 水印文字， 如：云账房
     * @param outImg 图片输出位置，如果为空，则覆盖原文件
     * @param fontName 字体名称，    如：宋体
     * @param fontStyle 字体样式，如：粗体和斜体(Font.BOLD|Font.ITALIC)
     * @param fontSize 字体大小，单位为像素
     * @param color 字体颜色
     * @param positionX 水印文字距离目标图片左侧的偏移量，如果x<0, 则在正中间
     * @param positionY 水印文字距离目标图片上侧的偏移量，如果y<0, 则在正中间
     * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明)
     * @param degree 水印图片旋转角度
     */
    public static void pressText(String targetImg, String pressText, String outImg, String fontName, int fontStyle, int fontSize, Color color, int positionX, int positionY, float alpha, Integer degree,String suffix) {
        try {
            File file = new File(targetImg);
            // 如果没有指定文件存放地址，则默认替换掉原图片
            File outFile;
            if (StrUtils.isEmpty(outImg)) {
                outFile = file;
            } else {
                outFile = new File(outImg);
            }
            Image image = ImageIO.read(file);
            int width = image.getWidth(null);
            int height = image.getHeight(null);
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Font font = new Font(fontName, fontStyle, fontSize);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image, 0, 0, width, height, null);
            g.setFont(font);
            g.setColor(color);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            if (null != degree) {
                // 设置水印旋转
                g.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
            }
            //获取文字所占的像素
            FontRenderContext context = g.getFontRenderContext();
            Rectangle2D stringBounds = font.getStringBounds(pressText,context);

            int textWidth = (int) stringBounds.getWidth() ;
            int textHeight = (int) stringBounds.getHeight();

            int widthDiff = width - textWidth;
            int heightDiff = height - textHeight;
            if (positionX < 0) {
                positionX = widthDiff / 2;
            } else if (positionX > widthDiff) {
                positionX = widthDiff;
            }
            if (positionY < 0) {
                positionY = heightDiff / 2;
            } else if (positionY > heightDiff) {
                positionY = heightDiff;
            }

            g.drawString(pressText, positionX, positionY + textHeight);
            g.dispose();
            ImageIO.write(bufferedImage, suffix, outFile);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加文字水印
     * @Description 坐标(0,0)在图片的左上角，(负数，负数)在中心位置，(0,xxx)在左边，(x,0)在最上边，其他位置根据图片来确定
     * @param targetImg 目标图片路径，如：C://myPictrue//1.jpg
     * @param pressText 水印文字， 如：云账房
     * @param fontName 字体名称，    如：宋体
     * @param fontStyle 字体样式，如：粗体和斜体(Font.BOLD|Font.ITALIC)
     * @param fontSize 字体大小，单位为像素
     * @param color 字体颜色
     * @param positionX 水印文字距离目标图片左侧的偏移量，如果x<0, 则在正中间
     * @param positionY 水印文字距离目标图片上侧的偏移量，如果y<0, 则在正中间
     * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明)
     * @param degree 水印图片旋转角度
     */
    public static InputStream pressText(String targetImg, String pressText, String fontName, int fontStyle, int fontSize, Color color, int positionX, int positionY, float alpha, Integer degree,String fileExtName) throws Exception{

        File file = new File(targetImg);
        Image image = ImageIO.read(file);
        int width = image.getWidth(null);
        int height = image.getHeight(null);
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Font font = new Font(fontName, fontStyle, fontSize);
        Graphics2D g = bufferedImage.createGraphics();
        g.drawImage(image, 0, 0, width, height, null);
        g.setFont(font);
        g.setColor(color);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
        if (null != degree) {
            // 设置水印旋转
            g.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
        }
        //获取文字所占的像素
        FontRenderContext context = g.getFontRenderContext();
        Rectangle2D stringBounds = font.getStringBounds(pressText,context);

        int textWidth = (int) stringBounds.getWidth() ;
        int textHeight = (int) stringBounds.getHeight();

        int widthDiff = width - textWidth;
        int heightDiff = height - textHeight;
        if (positionX < 0) {
            positionX = widthDiff / 2;
        } else if (positionX > widthDiff) {
            positionX = widthDiff;
        }
        if (positionY < 0) {
            positionY = heightDiff / 2;
        } else if (positionY > heightDiff) {
            positionY = heightDiff;
        }

        g.drawString(pressText, positionX, positionY + textHeight);
        g.dispose();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, fileExtName, os);
        InputStream input = new ByteArrayInputStream(os.toByteArray());
        return input;
    }

    /**
     * 添加文字水印
     * @Description 坐标(0,0)在图片的左上角，(负数，负数)在中心位置，(0,xxx)在左边，(x,0)在最上边，其他位置根据图片来确定
     * @param targetImg 目标图片路径，如：C://myPictrue//1.jpg
     * @param pressText 水印文字， 如：云账房
     * @param fontName 字体名称，    如：宋体
     * @param fontStyle 字体样式，如：粗体和斜体(Font.BOLD|Font.ITALIC)
     * @param fontSize 字体大小，单位为像素
     * @param color 字体颜色
     * @param positionX 水印文字距离目标图片左侧的偏移量，如果x<0, 则在正中间
     * @param positionY 水印文字距离目标图片上侧的偏移量，如果y<0, 则在正中间
     * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明)
     * @param degree 水印图片旋转角度
     */
    public static InputStream pressText(InputStream targetImg, String pressText, String fontName, int fontStyle, int fontSize, Color color, int positionX, int positionY, float alpha, Integer degree, String fileExtName) throws Exception{
        BufferedImage srcImg = ImageIO.read(targetImg);
        int width = srcImg.getWidth();
        int height = srcImg.getHeight();
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Font font = new Font(fontName, fontStyle, fontSize);
        Graphics2D g = bufferedImage.createGraphics();
        g.drawImage(srcImg, 0, 0, width, height, null);
        g.setFont(font);
        g.setColor(color);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
        if (null != degree) {
            // 设置水印旋转
            g.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
        }
        //获取文字所占的像素
        FontRenderContext context = g.getFontRenderContext();
        Rectangle2D stringBounds = font.getStringBounds(pressText,context);

        int textWidth = (int) stringBounds.getWidth() ;
        int textHeight = (int) stringBounds.getHeight();

        int widthDiff = width - textWidth;
        int heightDiff = height - textHeight;
        if (positionX < 0) {
            positionX = widthDiff / 2;
        } else if (positionX > widthDiff) {
            positionX = widthDiff;
        }
        if (positionY < 0) {
            positionY = heightDiff / 2;
        } else if (positionY > heightDiff) {
            positionY = heightDiff;
        }

        g.drawString(pressText, positionX, positionY + textHeight);
        g.dispose();
        ImageIO.write(bufferedImage, fileExtName, new File("/Users/mxli/Desktop/154_targer_mark.jpg"));
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, fileExtName, os);
        InputStream input = new ByteArrayInputStream(os.toByteArray());
        return input;
    }
    /**
     * 添加文字水印
     * @param targetImg 目标图片路径，如：C://myPictrue//1.jpg
     * @param pressText 水印文字， 如：云账房
     * @param fontName 字体名称，    如：宋体
     * @param fontStyle 字体样式，如：粗体和斜体(Font.BOLD|Font.ITALIC)
     * @param fontSize 字体大小，单位为像素
     * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明)
     */
    public static void pressTextToRightBottom(String targetImg, String pressText, String fontName, int fontStyle, int fontSize, Color color, float alpha,String suffix) {

        pressTextToRightBottom(targetImg,pressText,null,fontName,fontStyle,fontSize,color,alpha,null,suffix);
    }

    /**
     * 在图片的右下角添加水印
     *
     * @param targetImg 目标图片路径，如：C://myPictrue//1.jpg
     * @param pressText 水印文字， 如：云账房
     * @param outImg 图片输出位置，如果为空，则覆盖原文件
     * @param fontName 体名称， 如：宋体
     * @param fontStyle 字体样式，如：粗体和斜体(Font.BOLD|Font.ITALIC)
     * @param fontSize 字体大小，单位为像素
     * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明)
     * @param degree 水印图片旋转角度
     */
    public static void pressTextToRightBottom(String targetImg, String pressText, String outImg, String fontName, int fontStyle, int fontSize, Color color, float alpha, Integer degree,String suffix) {
        try {
            File file = new File(targetImg);
            File outFile;
            if (StrUtils.isEmpty(outImg)) {
                outFile = file;
            } else {
                outFile = new File(outImg);
            }
            Image image = ImageIO.read(file);
            int width = image.getWidth(null);
            int height = image.getHeight(null);
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Font font = new Font(fontName, fontStyle, fontSize);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image, 0, 0, width, height, null);
            g.setFont(font);
            g.setColor(color);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            if (null != degree) {
                // 设置水印旋转
                g.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
            }

            //获取文字所占的像素
            FontRenderContext context = g.getFontRenderContext();
            Rectangle2D stringBounds = font.getStringBounds(pressText,context);

            int textWidth = (int) stringBounds.getWidth() ;
            int textHeight = (int) stringBounds.getHeight();

            int widthDiff = width - textWidth;
            int heightDiff = height - textHeight;

            widthDiff = widthDiff / 2;
            heightDiff = heightDiff / 2;
            g.drawString(pressText, widthDiff, heightDiff);
            g.dispose();
            ImageIO.write(bufferedImage, suffix.substring(1), outFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 在图片的右下角添加水印
     *
     * @param srcInput 目标图片路径，如：C://myPictrue//1.jpg
     * @param pressText 水印文字， 如：云账房
     * @param fontName 体名称， 如：宋体
     * @param fontStyle 字体样式，如：粗体和斜体(Font.BOLD|Font.ITALIC)
     * @param fontSize 字体大小，单位为像素
     * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明)
     * @param degree 水印图片旋转角度
     */
    public static InputStream pressText(InputStream srcInput, String pressText, String fontName, int fontStyle, int fontSize, Color color, float alpha, Integer degree,String suffix) {
        try {
//            BufferedImage image = ImageIO.read(newInput);
            Image image = ImageIO.read(srcInput);
            int width = image.getWidth(null);
            int height = image.getHeight(null);
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Font font = new Font(fontName, fontStyle, fontSize);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image, 0, 0, width, height, null);
            g.setFont(font);
            g.setColor(color);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            if (null != degree) {
                // 设置水印旋转
                g.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
            }

            //获取文字所占的像素
            FontRenderContext context = g.getFontRenderContext();
            Rectangle2D stringBounds = font.getStringBounds(pressText,context);

            int textWidth = (int) stringBounds.getWidth() ;
            int textHeight = (int) stringBounds.getHeight();

            int widthDiff = width - textWidth;
            int heightDiff = height - textHeight;

            widthDiff = widthDiff / 2;
            heightDiff = heightDiff / 2;
            g.drawString(pressText, widthDiff, heightDiff);
            g.dispose();
//            ImageIO.write(bufferedImage, suffix, new File("/Users/mxli/Desktop/154_targer_mark.jpg"));
//            return null;
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, suffix, os);
            InputStream input = new ByteArrayInputStream(os.toByteArray());
            return input;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 在图片的右下角添加水印
     *
     * @param bytes 目标图片路径，如：C://myPictrue//1.jpg
     * @param pressText 水印文字， 如：云账房
     * @param fontName 体名称， 如：宋体
     * @param fontStyle 字体样式，如：粗体和斜体(Font.BOLD|Font.ITALIC)
     * @param fontSize 字体大小，单位为像素
     * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明)
     * @param degree 水印图片旋转角度
     */
    public static InputStream pressText(byte[] bytes, String pressText, String fontName, int fontStyle, int fontSize, Color color, float alpha, Integer degree,String suffix) {
        try {
            InputStream inputStream = new ByteArrayInputStream(bytes);
            Image image = ImageIO.read(inputStream);
            int width = image.getWidth(null);
            int height = image.getHeight(null);
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Font font = new Font(fontName, fontStyle, fontSize);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image, 0, 0, width, height, null);
            g.setFont(font);
            g.setColor(color);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            if (null != degree) {
                // 设置水印旋转
                g.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
            }

            //获取文字所占的像素
            FontRenderContext context = g.getFontRenderContext();
            Rectangle2D stringBounds = font.getStringBounds(pressText,context);

            int textWidth = (int) stringBounds.getWidth() ;
            int textHeight = (int) stringBounds.getHeight();

            int widthDiff = width - textWidth;
            int heightDiff = height - textHeight;

            widthDiff = widthDiff / 2;
            heightDiff = heightDiff / 2;
            g.drawString(pressText, widthDiff, heightDiff);
            g.dispose();
//            ImageIO.write(bufferedImage, suffix, new File("/Users/mxli/Desktop/154_targer_mark.jpg"));
//            return null;
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, suffix, os);
            InputStream input = new ByteArrayInputStream(os.toByteArray());
            return input;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 添加图片水印
     * @param srcUrl 目标图片路径，如：C://myPictrue//1.jpg
     * @param waterUrl 水印图片路径，如：C://myPictrue//logo.png
     * @param targerUrl 图片输出位置，如果为空，则覆盖原文件
     * @param x 水印图片距离目标图片左侧的偏移量，如果x<0, 则在正中间
     * @param y 水印图片距离目标图片上侧的偏移量，如果y<0, 则在正中间
     * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明)
     * @param degree 水印图片旋转角度
     */
    public static void markImage(String srcUrl, String waterUrl, String targerUrl, int x, int y, float alpha, Integer degree, String fileExtName) {
        try {
            File srcFile = new File(srcUrl);
            //如果没有指定文件存放地址，则默认替换掉原图片
            File targerFile;
            if(StrUtils.isEmpty(targerUrl)) {
                targerFile = srcFile;
            } else {
                targerFile = new File(targerUrl);
            }

            BufferedImage image = ImageIO.read(srcFile);

            int srcWidth = image.getWidth();
            int srcHeight = image.getHeight();
            BufferedImage bufferedImage = new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image, 0, 0, srcWidth, srcHeight, null);

            if (null != degree) {
                // 设置水印旋转
                g.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
            }

            BufferedImage waterImage = ImageIO.read(new File(waterUrl));    // 水印文件
            int waterWidth = waterImage.getWidth();
            int waterHeight = waterImage.getHeight();
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

            int widthDiff = srcWidth - waterWidth;
            int heightDiff = srcHeight - waterHeight;
            if (x < 0) {
                x = widthDiff / 2;
            } else if (x > widthDiff) {
                x = widthDiff;
            }
            if (y < 0) {
                y = heightDiff / 2;
            } else if (y > heightDiff) {
                y = heightDiff;
            }
            g.drawImage(waterImage, 0, 0, widthDiff, heightDiff, null); // 水印文件结束
            g.dispose();
            //注意这里的后缀不能带 .
            ImageIO.write(bufferedImage, fileExtName, targerFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 把图片印刷到图片上
     */
    public final static void pressImage(String srcUrl, String waterUrl, String targerUrl) {
        try {
            //目标文件
            File _file = new File(srcUrl);
            Image src = ImageIO.read(_file);
            int wideth = src.getWidth(null);
            int height = src.getHeight(null);
            BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
            Graphics g = image.createGraphics();
            g.drawImage(src, 0, 0, wideth, height, null);

            //水印文件
            File _filebiao = new File(waterUrl);
            Image src_biao = ImageIO.read(_filebiao);
            int wideth_biao = src_biao.getWidth(null);
            int height_biao = src_biao.getHeight(null);
            g.drawImage(src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wideth_biao, height_biao, null);
            //水印文件结束
            g.dispose();
//            FileOutputStream out = new FileOutputStream(targerUrl);
//            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//            encoder.encode(image);
//            out.close();

            ImageIO.write(image, "jpeg", new File(targerUrl));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 给图片添加水印、可设置水印图片位置与旋转角
     * @param iconPath      水印图片路径
     * @param srcImgPath    源图片路径
     * @param targerPath    目标图片路径
     * @param location      水印位置：0、自定义 1、左上角，2、右上角，3、左下角，4、右下角，5、中间
     * @param degree        旋转角：顺时针角度
     */
    public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath, Integer location, Integer degree) {
        OutputStream os = null;
        try {
            Image srcImg = ImageIO.read(new File(srcImgPath));

            int width = srcImg.getWidth(null);
            int height = srcImg.getHeight(null);

            BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            // 得到画笔对象
            Graphics2D g = buffImg.createGraphics();

            // 设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

            g.drawImage(srcImg, 0, 0, width, height, null);

            if (null != degree) {
                // 设置水印旋转
                g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
            }

            // 水印图象的路径 水印一般为gif或者png的，这样可设置透明度
            ImageIcon imgIcon = new ImageIcon(iconPath);

            // 得到Image对象。
            Image syImg = imgIcon.getImage();

            float alpha = 0.5f; // 透明度

            int syWidth = syImg.getWidth(null);
            int syHeight = syImg.getHeight(null);

            // 如果水印图片高或宽大于目标图片是做的处理，使水印宽或高等于目标图片的宽高，并且等比例缩放
            int newSyWidth = syWidth;
            int newSyHeight = syHeight;

            if (syWidth > width) {
                newSyWidth = width;
                newSyHeight = (int) ((double)newSyWidth / syWidth * height);
            }

            if (newSyHeight > height) {
                newSyHeight = height;
                newSyWidth = (int) ((double)newSyHeight / syHeight * newSyWidth);
            }

            // 根据位置参数确定坐标位置
            int x = 0, y = 0;
            // location 位置： 1、左上角，2、右上角，3、左下角，4、右下角，5、中间
            switch (location) {
                case 1: break;
                case 2:
                    x = width - newSyWidth;
                    break;
                case 3:
                    y = height - newSyHeight;
                    break;
                case 4:
                    x = width - newSyWidth;
                    y = height - newSyHeight;
                    break;
                case 5:
                    x = (width - newSyWidth) / 2;
                    y = (height - newSyHeight) / 2;
                    break;
                default: break;
            }

            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

            // 表示水印图片的位置
            g.drawImage(syImg, x, y, newSyWidth, newSyHeight, null);

            // 水印文件结束
            g.dispose();

            os = new FileOutputStream(targerPath);

            // 生成图片
            ImageIO.write(buffImg, "JPG", os);

            log.info("图片完成添加Icon印章... srcImgPath:{}, targerPath:{}", srcImgPath, targerPath);
        } catch (Exception e) {
            log.info("图片添加Icon印章异常！ iconPath:{}, srcImgPath:{}, targerPath:{}, location:{}", iconPath, srcImgPath, targerPath, location);
        } finally {
            try {
                if (null != os) {
                    os.close();
                }
            } catch (Exception e) {
                log.error("", e);
            }
        }
    }


    public static void main(String[] args) {
//        Color color = new Color(248,248,255);
//        pressTextToRightBottom("/Users/mxli/Desktop/154.jpg", "中铁工服", "/Users/mxli/Desktop/154_targer_mark.jpg", "宋体", Font.BOLD, 22, Color.RED, 0.5f, -30, ".jpg");
//        pressText("/Users/mxli/Desktop/timg.jpeg", "中铁工服", "/Users/mxli/Desktop/timg_mark.jpeg", "宋体", Font.BOLD, 22, Color.RED, -1, -1, 0.5f, 120, ".jpeg");
        String srcUrl = "/Users/mxli/Desktop/timg.jpeg";
        String targerUrl = "/Users/mxli/Desktop/154_targer_mark.jpg";
        File targerFile = new File(targerUrl);
        String fileExtName = "jpg";
        File file = new File(srcUrl);
        try {
            InputStream input = new FileInputStream(file);
            InputStream newInput = pressText(input, "中铁工服", "宋体", Font.BOLD, 22, Color.RED, 0.5f, -30, fileExtName);
            BufferedImage image = ImageIO.read(newInput);

            ImageIO.write(image, fileExtName, targerFile);
        } catch (Exception e) {
            e.printStackTrace();
        }

//
//        String s = "/Users/mxli/Desktop/154_targer.jpg";
//        String s1 = s.substring(1, s.indexOf("/",2));
//        String s2 = s.substring(s.indexOf("/",2));
//        System.out.println(s1+"-------"+s2);
    }
}